package Kuaishou;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.collect.Lists;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2020-12-25
 */
public class Test17并行计算5 {
    public static void main(String[] args) {
        List<Long> list = Lists.newArrayList();
        for (long i = 1; i <= 50000; i++) {
            list.add(i);
        }
        AtomicLong sum = new AtomicLong();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        long start = System.currentTimeMillis();
        forkJoinPool.submit(() -> {
            System.out.println(Thread.currentThread().getName()+"start");
            list.parallelStream()
                    .forEach(num -> {
                        sum.addAndGet(num);
                        if ((sum.get() % 10000) == 0) {
                            System.out.println(
                                    Thread.currentThread().getName() + "耗时:" + (System.currentTimeMillis() - start
                                            + "ms，结果:" + sum.get()));
                        }
                    });
            System.out.println(Thread.currentThread().getName()+"总耗时1:"+ (System.currentTimeMillis() - start
                    +"ms，结果:"+ sum.get()));
        }).join();
        System.out.println(Thread.currentThread().getName()+"总耗时2:"+ (System.currentTimeMillis() - start
                +"ms，结果:"+ sum.get()));
    }
}
